package com.txunda.zhuohired.adapter.user.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.bean.user.home.CityBean;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2017/11/21 10:48
 * 功能描述：选择城市adapter
 * 联系方式：jiahuanchen@126.com
 */

public class SelectCityAdapter extends BaseAdapter {
    private Context context;
    private List<CityBean.DataBean> cityList;
    private ViewHolder holder;

    public SelectCityAdapter(Context context, List cityList) {
        this.context = context;
        this.cityList = cityList;
    }

    @Override
    public int getCount() {
        return cityList.size();
    }

    @Override
    public Object getItem(int i) {
        return cityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_select_city, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        int section = getSectionForPosition(i);

        if (i == getPositionForSection(section)) {
            holder.tv_select_city.setVisibility(View.VISIBLE);
            holder.tv_select_city.setText(cityList.get(i).getSortLetters());
        } else {
            holder.tv_select_city.setVisibility(View.GONE);
        }

        holder.tv_select_city_name.setText(cityList.get(i).getRegion_name());
        return view;
    }

    public int getSectionForPosition(int position) {
        return cityList.get(position).getSortLetters().charAt(0);
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = cityList.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    class ViewHolder {
        @ViewInject(R.id.tv_select_city)
        private TextView tv_select_city;//首字母
        @ViewInject(R.id.tv_select_city_name)
        private TextView tv_select_city_name;//城市名称
    }

}
